package com.tony.demo.widget;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tony.demo.R;
import com.tony.demo.interfaces.OnDialogClickListener;

public class MyDialog extends DialogFragment {
	/**
	 * if you want create special dialog, you should define unrepeatable type value
	 */
	private static final String ARGUMENT_KEY_TYPE = "type";// dialog id
	private static final String ARGUMENT_KEY_ICON = "icon";// dialog icon
	private static final String ARGUMENT_KEY_TITLE = "title";// dialog title
	private static final String ARGUMENT_KEY_MESSAGE = "message";// dialog content

	public static final int DIALOT_TYPE_WAITING = 50;// waiting dialog type

	private OnDialogClickListener mListener;

	private void setClickListener(OnDialogClickListener listener) {
		mListener = listener;
	}

	private OnDialogClickListener getClickListener() {
		return mListener;
	}

	public static MyDialog createDialog(OnDialogClickListener listener, int id, int title, int message) {
		return createDialog(listener, id, 0, title, message);
	}

	public static MyDialog createDialog(OnDialogClickListener listener, int type, int icon, int title, int message) {
		MyDialog dialog = new MyDialog();

		Bundle bundle = new Bundle();
		bundle.putInt(ARGUMENT_KEY_TYPE, type);
		bundle.putInt(ARGUMENT_KEY_ICON, icon);
		bundle.putInt(ARGUMENT_KEY_TITLE, title);
		bundle.putInt(ARGUMENT_KEY_MESSAGE, message);

		dialog.setArguments(bundle);
		dialog.setClickListener(listener);
		dialog.setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Light_Dialog);
		return dialog;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		final int type = getArguments().getInt(ARGUMENT_KEY_TYPE);
		// type = DialogFragment.STYLE_NORMAL;
		int title = getArguments().getInt(ARGUMENT_KEY_TITLE);
		int message = getArguments().getInt(ARGUMENT_KEY_MESSAGE);

		switch (type) {
			case DialogFragment.STYLE_NORMAL:

			default:
				AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
				dialog.setIcon(R.drawable.ic_launcher);
				dialog.setTitle(title);
				dialog.setMessage(message);
				dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						getClickListener().onPositiveClick(type);
					}
				});
				dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						getClickListener().onNegativeClick(type);
					}
				});
				return dialog.create();
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		final int type = getArguments().getInt(ARGUMENT_KEY_TYPE);
		int icon = getArguments().getInt(ARGUMENT_KEY_ICON);
		int title = getArguments().getInt(ARGUMENT_KEY_TITLE);
		int message = getArguments().getInt(ARGUMENT_KEY_MESSAGE);

		View view = null;
		switch (type) {
			case DIALOT_TYPE_WAITING:
				// view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_progress_waiting_layout, container, false);
				// TextView msg = (TextView) view.findViewById(R.id.waiting_message);
				// msg.setText(message);
				return view;

				// case DialogFragment.STYLE_NORMAL:
				// view = LayoutInflater.from(getActivity()).inflate(R.layout.aboutview, container, false);
			default:
				// Button btn = (Button) view.findViewById(R.id.btn_cancel);
				// btn.setOnClickListener(new View.OnClickListener() {
				// @Override
				// public void onClick(View v) {
				// getClickListener().onPositiveClick(type);
				// }
				// });

				return super.onCreateView(inflater, container, savedInstanceState);
		}
	}

	@Override
	public void onDismiss(DialogInterface dialog) {
		getClickListener().onDialogDismiss(getArguments().getInt(ARGUMENT_KEY_TYPE));

		super.onDismiss(dialog);
	}
}
